const ResponseModel = require('../models/response-model')
const SystemConfig = require('../utils/system-config')
const SiteModel = require('../models/btsite-model')
const TorrentModel = require('../models/torrent-model')
const Search = require('../utils/search.js')

module.exports = {
    // rss 获取
    'get::/search/rss': async function(ctx) {
        
        const params = ctx.request.query

        let data = []
        if (params.siteid) {
            const model = await SiteModel.getById(params.siteid)
            if (model && model.rss && (model.rss.startsWith('https://') || model.rss.startsWith('http://'))) {
                const list = await Search.rss(model.rss, params)
                // 查询是否添加到系统
                if (list.length > 0) {
                    const hashList = list.map(row => row.hash)
                    const whereBuilder = countQuery => {
                        countQuery.where('hash_string', 'in', hashList)
                    }
                    const existList = await TorrentModel.list(TorrentModel.buildPaging({pageSize: list.length}) ,whereBuilder)
                    data = list.map(row => ({
                        ...row,
                        isAdd: existList.find(item => item.hashString === row.hash) ? true : false
                    }))
                }
            }
        }
        ctx.response.body = ResponseModel.success(data)
    },
    // 
    'get::/search/jackett': async (ctx) => {
        const systemConfig = await SystemConfig.me();
        // console.log(systemConfig);

        const params = ctx.request.query

        let data = {
            Indexers: [],
            Results: []
        };
        if (params.keyword) {
            if (systemConfig.jackett && (systemConfig.jackett?.url.startsWith('https://') || systemConfig.jackett?.url.startsWith('http://'))) {
                const res = await Search.jackett(systemConfig.jackett?.url, systemConfig.jackett?.apiKey, params)
                if (res) {
                    // 查询是否添加到系统
                    const list = res.Results;
                    if (list.length > 0) {
                        const hashList = list.map(row => row.InfoHash)
                        const whereBuilder = countQuery => {
                            countQuery.where('hash_string', 'in', hashList)
                        }
                        const existList = await TorrentModel.list(TorrentModel.buildPaging({pageSize: list.length}) ,whereBuilder)
                        data.Indexers = res.Indexers
                        data.Results = res.Results.map(row => ({
                            ...row,
                            isAdd: existList.find(item => item.hashString === row.InfoHash) ? true : false
                        }))
                    }
                }
                ctx.response.body = ResponseModel.success(data)
            } else {
                ctx.response.body = ResponseModel.failure('未配置jackett服务器')
            }
        } else {
            ctx.response.body = ResponseModel.failure('参数错误')
        }
    },
}
  